<?php
require_once("./includes/config.inc.php");
$_SESSION['showPrevious'] = true;
$_SESSION['showNext'] = true;
$answer = $_REQUEST['answer'];
$sp = $_REQUEST['sp'];

/*
if($answer){
	$_SESSION['arrExamQuestions'][$_SESSION['lastQuestionShownIs']][] = $answer;
}else{
	if($_REQUEST['sp'] == 'fp'){
		// This is for final submission and result calculation
		echo "Thanks for taking this exam. Result is: <br>";
		$correctAnswer = 0;
		foreach($_SESSION['arrExamQuestions'] as $key => $value){
			if($value[5] == $value[6]){
				$correctAnswer += 1;
			}else{
				$wrongAnswer +=1;
			}
		}
		echo 'You have scored '.$correctAnswer.' out of '.($correctAnswer + $wrongAnswer);
	}else{
		echo '<pre>';
		//$arrPosition = array_keys($_SESSION['arrDisplayedQuestions']);
		//echo '<br> ======= Keys Position ======== <br> ';
		//print_r($arrPosition);
		//$lastShownPosition = array_search($_SESSION['lastQuestionShownIs'],$arrPosition);
		//echo "------".$lastShownPosition; 
		//$keyValue = $arrPosition[$lastShownPosition-1];
		
		
		
		if($_REQUEST['sp'] == 'sp'){
			$showThisQuestion = $arrPosition[$lastShownPosition-1];
		}else{
			$tmpArray = array_diff_key($_SESSION['arrExamQuestions'],$_SESSION['arrDisplayedQuestions']);
		//	echo '<br> ================ <br> Questions still need to be shown <br> ================ <br> ';
		//	print_r($tmpArray);
		//	echo "".count($tmpArray);
			
			if(count($tmpArray)){
				$showThisQuestion = array_rand($tmpArray,1);
			}else{
				$showThisQuestion = $arrPosition[$lastShownPosition+1];
			}
			
			
		//	echo "*****************".count($_SESSION['arrExamQuestions']);
			if(($lastShownPosition + 2) == count($_SESSION['arrExamQuestions'])){
				$_SESSION['showNext'] = false;
			}
		}	
		
		if($showThisQuestion){
			$_SESSION['arrDisplayedQuestions'][$showThisQuestion] = $showThisQuestion ;
			$_SESSION['lastQuestionShownIs'] = $showThisQuestion;
		}
	
	
		$arrPosition = array_keys($_SESSION['arrDisplayedQuestions']);
		//echo '<br> ======= Keys Position ======== <br> ';
		//print_r($arrPosition);
		$currentPosition = array_search($_SESSION['lastQuestionShownIs'],$arrPosition);
		//echo "------".$lastShownPosition; 
		//$keyValue = $arrPosition[$lastShownPosition-1];
		if($currentPosition == 0){
			$_SESSION['showPrevious'] = false;
		}
	
		//echo '<br> ================ <br> Questions which are displayed <br> ================ <br> ';
		//print_r($_SESSION['arrDisplayedQuestions']);
		//print_r($_SESSION['arrExamQuestions']);
	}
}
*/

if($_REQUEST['sp'] == 'fp'){
	// This is for final submission and result calculation
	$correctAnswer = 0;
	$_SESSION['arrExamQuestions'][$_SESSION['lastQuestionShownIs']][] = $answer;
	foreach($_SESSION['arrExamQuestions'] as $key => $value){
		if($value[5] == $value[6]){
			$correctAnswer += 1;
		}else{
			$wrongAnswer +=1;
		}
	}

	$strHtml = '<table width="95%" align="center" cellpadding="10"  cellspacing="0" bgcolor="#ffffff" class="border">
  <tr>
	<td align="left" height="1px"></td>
  </tr>
  <tr>
	<td class="heading-bold-n" align="left">Thanks for taking the Exam </td>
  </tr>
  <tr>
	<td class="txt3a" style="padding:0px 0px 5px 90px;">Your Score is '.$correctAnswer.' out of '.($correctAnswer + $wrongAnswer).' </td>
  </tr>
</table>
';
	echo $strHtml;
	//echo "Thanks for taking this exam. Result is: <br>";
	//echo 'You have scored '.$correctAnswer.' out of '.($correctAnswer + $wrongAnswer);
}else{
	if($sp == 'sp'){
		$_SESSION['sessCurIndex'] = $_SESSION['sessCurIndex']-1;
	}
	
	if($answer and $sp != 'sp'){
		$_SESSION['arrExamQuestions'][$_SESSION['lastQuestionShownIs']][6] = $answer; // 6th is the index place where 
																					  // answer is stored
		$_SESSION['sessCurIndex'] = $_SESSION['sessCurIndex']+1;
	}else{
		// This will be executed for the very first time this page is load.
		if(!$_SESSION['arrKeys']){
			$_SESSION['arrKeys'] = array_keys($_SESSION['arrExamQuestions']);
			$_SESSION['sessCurIndex'] = 0;
		}
	
	}
	
	$showThisQuestion = $_SESSION['arrKeys'][$_SESSION['sessCurIndex']];
	$_SESSION['lastQuestionShownIs'] = $showThisQuestion;
}
?>